package com.chen.helper;


import android.support.annotation.ColorInt;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.loadmore.SimpleLoadMoreView;
import com.chen.helper.widget.RefreshRecycleView;

import java.util.ArrayList;
import java.util.List;

/**
 * LightRecycleViewHelper
 * 可选分页加载,下拉刷新
 */

public abstract class LightRecycleViewHelper<T> extends AbstractRecycleViewHelper<T> {

    private RefreshRecycleView refreshRecycleView;

    /**
     * 设置pageNum, 1.2.3...
     */
    private int pageNum;
    /**
     * 起始位置 默认 为 0, 可能 为 1
     */
    private int start = 0;
    /**
     * 请求的最后一页的PageNum
     */
    private int lastPageNum;
    /**
     * 当前请求到的数据量,如果该数量小于 pageSize,说明,加载更多已经完成
     */
    private int currentLoadSize = 0;

    /**
     * true: 加载跟多
     * false:加载一页 :false的意义:增加手动刷新的动作
     */
    private final boolean loadMore;


    private SwipeRefreshLayout srl;


    public LightRecycleViewHelper setPageSize(int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    public int getPageSize() {
        return pageSize;
    }

    /**
     * 默认 10条
     */

    private int pageSize = 10;

    public LightRecycleViewHelper(int resId, final RefreshRecycleView rv) {

        this(resId, rv, true);
    }


    /**
     * @param resId    资源id
     * @param rv       RefreshRecycleView
     * @param loadMore 是否加载更多
     */
    public LightRecycleViewHelper(int resId,
                                  final RefreshRecycleView rv,
                                  boolean loadMore) {

        super(resId, rv.getRecyclerView());
        refreshRecycleView = rv;
        this.loadMore = loadMore;
        initView();
    }


    /**
     * 普通的recyclerview
     *
     * @param resId
     * @param rv
     */
    public LightRecycleViewHelper(int resId, final RecyclerView rv) {
        super(resId, rv);
        loadMore = false;

    }


    private void initView() {
        this.srl = refreshRecycleView.getSwipeRefreshLayout();
        if (loadMore) {
            adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {
                    //最后一页,完成加载
                    if (isLastPage()) {
                        adapter.loadMoreEnd();
                        return;
                    }
                    pageNum++;
                    requestListData();
                }
            }, refreshRecycleView.getRecyclerView());
        }
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pullRefresh();

            }
        });
        if (loadMore) {
            // 使用系统的loadmoreview
            adapter.setLoadMoreView(new SimpleLoadMoreView());
        }

    }

    /**
     * 下拉请求刷新
     */
    protected void pullRefresh() {
        requestFirstPage();
    }


    public void requestFirstPage() {
        resetAndRequest();
    }


    /**
     * 请求 ListData
     */
    private void requestListData() {
        if (runnable != null) {
            runnable.run();
        }

    }

    /**
     * setFirstPageNum
     *
     * @param start
     * @return
     */
    public LightRecycleViewHelper setFirstPageNum(int start) {
        this.start = start;
        pageNum = start;
        return this;
    }

    @Override
    public void setListData(List<T> itemData) {
        if (itemData == null) {
            itemData = new ArrayList<>();
        }
        hideRefresh();
        if (!loadMore) {
            super.setListData(itemData);
            return;
        }
        //当前请求到的页数
        currentLoadSize = itemData.size();
        if (isFirstPage()) {
            adapter.setNewData(itemData);

        } else {
            adapter.loadMoreComplete();
            adapter.addData(itemData);
        }

    }

    private void hideRefresh() {
        if (srl != null) {
            srl.setRefreshing(false);
        }
    }

    public void setError() {
        hideRefresh();
        if (!loadMore) {
            super.setError();
            return;
        }
        adapter.loadMoreFail();
    }

    /**
     * 重置 ,并请求
     *
     * @return
     */
    private void resetAndRequest() {
        resetParam();
        requestListData();

    }

    /**
     * 重置 相关字段
     */
    public final void resetParam() {
        currentLoadSize = 0;
        pageNum = start;
        adapter.loadMoreComplete();
    }


    public boolean isFirstPage() {
        return pageNum == start;
    }

    public int getPageNum() {
        return pageNum;
    }


    public int getDataSize() {
        return getDataList().size();
    }

    public List<T> getDataList() {
        return adapter.getData();
    }


    public void setRefresh(boolean res) {
        if (srl != null) {
            srl.setRefreshing(res);
        }

    }

    public void setSwipeColor(@ColorInt int... color) {
        if (srl != null) {
            srl.setColorSchemeColors(color);
        }

    }

    public boolean isRefreshing() {
        if (srl != null) {
            return srl.isRefreshing();
        }
        return false;

    }

    /**
     * 是否去掉刷新view,如果去掉刷新view,则加载更多
     *
     * @param enable
     */
    public void disableRefreshView(boolean enable) {
        if (srl != null) {
            srl.setEnabled(enable);
        }

    }

    private Runnable runnable;

    /**
     * 请求数据的callback,如果不是带刷新的,则不处理
     *
     * @param runnable
     */
    public void postData(Runnable runnable) {
        if (refreshRecycleView != null) {
            this.runnable = runnable;
        }

    }


    /**
     * 获取最后一页 的 pageNum
     */
    public void setLastPageNum(int totalCount) {
        //20%10 ->left ->0 ,lastPageNum 20/10 +0= 2
        //21%10 ->left ->1 lastPageNum 21/10 +1 = 3
        //19%10 ->left ->1 lastPageNum 19/10 +1 = 2
        int left = totalCount % pageSize > 0 ? 1 : 0;
        // start 0 ->lastPageNum-1
        //start 1->lastPageNum
        lastPageNum = totalCount / pageSize + left + start - 1;

    }

    /**
     * 是否到了最后一页,
     * 1.当前加载的小于 pageSize
     * 2.pageNum>=请求次数
     * 例如: pageZie: 10 ,total = 115 条, start = 0, 计算, lastPageNum = 11,
     * 请求后台时, pageNum > lastPageNum,有些后台会请求 lastPageNum,所以要对 pageNum>=请求次数 做个判断
     *
     * @return true: 已经到了最后一页
     */
    private boolean isLastPage() {
        return currentLoadSize < pageSize || pageNum >= lastPageNum;
    }
}
